home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / NString 1.0 beta / Sources / NString_Constructor.cxx < prev    next >
Encoding:
C/C++ Source or Header  |  1994-11-16  |  3.1 KB  |  153 lines  |  [TEXT/KAHL]

  1.  
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <float.h>
  5.  
  6. #if NSTRING_DEBUG > 0
  7.     #include <stdio.h>
  8. #endif
  9.  
  10. #include "NString.h"
  11. #include "NString_Misc.h"
  12.  
  13. //_________________________________________________________________________________
  14.  
  15. int NString::SetBufferSizeStep (unsigned long int NewSizeStep)
  16. {
  17.     if (NewSizeStep == 0)
  18.         return 0;
  19.     BufferSizeStep = NewSizeStep;
  20.     DeallocationMargin = (unsigned long int)(NewSizeStep * DeallocationTrigger);
  21.     return 1;
  22. }
  23.  
  24. //_________________________________________________________________________________
  25.  
  26. unsigned long int NString::GetBufferSizeStep (void)
  27. {
  28.     return BufferSizeStep;
  29. }
  30.  
  31. //_________________________________________________________________________________
  32.  
  33. int NString::SetDeallocationTrigger (float NewValue)
  34. {
  35.     if ((NewValue < 0.0) || (NewValue > (0.99 + FLT_EPSILON)))
  36.         return 0;
  37.     DeallocationTrigger = NewValue;
  38.     DeallocationMargin = (unsigned long int)(BufferSizeStep * NewValue);
  39.     return 1;
  40. }
  41.  
  42. //_________________________________________________________________________________
  43.  
  44. float NString::GetDeallocationTrigger (void)
  45. {
  46.     return DeallocationTrigger;
  47. }
  48.  
  49. //_________________________________________________________________________________
  50.  
  51. NString::NString (void)
  52. {
  53.     const char *fname = "NString (void)";
  54.  
  55.     if ((sb = new strbody) == NULL)
  56.         OUT_OF_MEM(fname);
  57.     if (! AllocStrBuf(0))
  58.     {
  59.         delete sb;
  60.         sb = NULL;
  61.         OUT_OF_MEM(fname);
  62.     }
  63.     sb->str[0] = '\0';
  64.     
  65. #if (NSTRING_DEBUG & 2) != 0
  66.     printf("Construction of empty NString.\n");
  67. #endif
  68. }
  69.  
  70. //_________________________________________________________________________________
  71.  
  72. NString::NString (const char *s)
  73. {
  74.     const char *fname = "NString (const char *)";
  75.  
  76.     if ((sb = new strbody) == NULL)
  77.         OUT_OF_MEM(fname);
  78.     if ((s != NULL) && (s[0] != '\0'))                            // non-empty string given -> copy it
  79.     {
  80.         if (! AllocStrBuf(strlen(s)))
  81.         {
  82.             delete sb;
  83.             sb = NULL;
  84.             OUT_OF_MEM(fname);
  85.         }
  86.         strcpy(sb->str, s);
  87.     }
  88.     else                                                                        // empty string or NULL given -> create empty NString
  89.     {
  90.         if (! AllocStrBuf(0))
  91.         {
  92.             delete sb;
  93.             sb = NULL;
  94.             OUT_OF_MEM(fname);
  95.         }
  96.         sb->str[0] = '\0';
  97.     }
  98.     
  99. #if (NSTRING_DEBUG & 2) != 0
  100.     printf("Construction of NString \"%s\" from a C-String.\n", sb->str);
  101. #endif
  102. }
  103.  
  104. //_________________________________________________________________________________
  105.  
  106. NString::NString (const char c)
  107. {
  108.     const char *fname = "NString (const char)";        // the method's name (for error handling purposes)
  109.  
  110.     if (c == '\0')
  111.         USAGE_ERR(fname, "NString construction from NUL character attempted");
  112.     if ((sb = new strbody) == NULL)
  113.         OUT_OF_MEM(fname);
  114.     if (! AllocStrBuf(1))
  115.         OUT_OF_MEM(fname);
  116.     sb->str[0] = c;
  117.     sb->str[1] = '\0';
  118.     
  119. #if (NSTRING_DEBUG & 2) != 0
  120.     printf("Construction of NString \"%s\" from a character.\n", sb->str);
  121. #endif
  122. }
  123.  
  124. //_________________________________________________________________________________
  125.  
  126. NString::~NString (void)
  127. {
  128.  
  129. #if (NSTRING_DEBUG & 2) != 0
  130.     printf("Destruction of NString \"%s\"", sb->str);
  131. #endif
  132.  
  133.     if (sb->refs <= 1)
  134.     {
  135.         free(sb->str);
  136.         delete sb;
  137.  
  138.     #if (NSTRING_DEBUG & 2) != 0
  139.          printf(" - Deleted string body.\n");
  140.      #endif
  141.      
  142.     }
  143.     else
  144.     {
  145.         sb->refs--;
  146.  
  147.     #if (NSTRING_DEBUG & 2) != 0
  148.         printf(".\n");
  149.     #endif
  150.  
  151.     }
  152. }
  153.